In [1]:
%matplotlib inline
import cPickle
import pickle
import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from termcolor import colored

In [2]:
def plot_confusion_matrix(y_test,y_pred, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    import pandas as ps
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    from sklearn.metrics import confusion_matrix
    cm = confusion_matrix(y_test, y_pred)
    
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    pd.DataFrame(cm).to_csv("confusion_2.csv", sep=",",header = list(classes))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

In [3]:
import numpy as np
import re
from scipy import linalg
import scipy.ndimage as ndi
from six.moves import range
import os
import threading

def transform_matrix_offset_center(matrix, x, y):
    o_x = float(x) / 2 + 0.5
    o_y = float(y) / 2 + 0.5
    offset_matrix = np.array([[1, 0, o_x], [0, 1, o_y], [0, 0, 1]])
    reset_matrix = np.array([[1, 0, -o_x], [0, 1, -o_y], [0, 0, 1]])
    transform_matrix = np.dot(np.dot(offset_matrix, matrix), reset_matrix)
    return transform_matrix

def apply_transform(x, transform_matrix, channel_index=2, fill_mode='nearest', cval=0.):
    x = np.rollaxis(x, channel_index, 0)
    final_affine_matrix = transform_matrix[:2, :2]
    final_offset = transform_matrix[:2, 2]
    channel_images = [ndi.interpolation.affine_transform(x_channel, final_affine_matrix,
                      final_offset, order=0, mode=fill_mode, cval=cval) for x_channel in x]
    x = np.stack(channel_images, axis=0)
    x = np.rollaxis(x, 0, channel_index+1)
    return x


def image_rotation(x, rg,step, row_index=0, col_index=1, channel_index=2,
                    fill_mode='nearest', cval=0.):
    image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
    for i in range(-rg,rg,step):
        theta = (np.pi  * i)/ 180
        rotation_matrix = np.array([[np.cos(theta), -np.sin(theta), 0],
                                [np.sin(theta), np.cos(theta), 0],
                                [0, 0, 1]])

        h, w = x.shape[row_index], x.shape[col_index]
        transform_matrix = transform_matrix_offset_center(rotation_matrix, h, w)
        new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
        image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
    return image_set

def image_shear(x,  row_index=0, col_index=1, channel_index=2,
                 fill_mode='nearest', cval=0.):
    shear = [-0.25,0.25,6.3,9.5,-9.5,2.95,-2.95,0]
    image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
    for i in shear:
        shear_matrix = np.array([[1, -np.sin(i), 0],
                             [0, np.cos(i), 0],
                             [0, 0, 1]])
        h, w = x.shape[row_index], x.shape[col_index]
        transform_matrix = transform_matrix_offset_center(shear_matrix, h, w)
        new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
        image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
    return image_set[1:]



def image_zoom(x, zoom_range,step, row_index=0, col_index=1, channel_index=2,
                fill_mode='nearest', cval=0.):
    if len(zoom_range) != 2:
        raise Exception('zoom_range should be a tuple or list of two floats. '
                        'Received arg: ', zoom_range)

    if zoom_range[0] == 1 and zoom_range[1] == 1:
        zx, zy = 1, 1
    else:
        image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
        for zx in np.arange(zoom_range[0],zoom_range[1],step):
            for zy in np.arange(zoom_range[0],zoom_range[1],step):
                zoom_matrix = np.array([[zx, 0, 0],
                            [0, zy, 0],
                            [0, 0, 1]])

                h, w = x.shape[row_index], x.shape[col_index]
                transform_matrix = transform_matrix_offset_center(zoom_matrix, h, w)
                new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
                image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
    return image_set[1:]

def image_shift(x, wrg, hrg,step ,row_index=0, col_index=1, channel_index=2,
                 fill_mode='nearest', cval=0.):
    h,w = x.shape[row_index],x.shape[col_index]
    image_set = np.zeros((np.expand_dims(x,axis=0).shape),dtype=np.uint8)
    for tx in np.arange(-hrg*h,hrg*h,step):
        for ty in np.arange(-wrg*h,wrg*h,step):
            translation_matrix = np.array([[1, 0, tx],
                                   [0, 1, ty],
                                   [0, 0, 1]])

            transform_matrix = translation_matrix  # no need to do offset
            new = apply_transform(x, transform_matrix, channel_index, fill_mode, cval)
            image_set = np.vstack((image_set,np.expand_dims(new,axis=0)))
    return image_set[1:]

In [14]:
def load_data():
    import pandas as pd
    import numpy as np
    from PIL import Image
    from cv2 import resize
    #from skimage.transform import resize
    from skimage import exposure
    
    train = pd.read_csv('/home/mckc/Downloads/bigdb////train.csv')
    test = pd.read_csv('/home/mckc/Downloads/bigdb////test.csv')
    
    #train = train.query('subject in ("Dhruva Murari","Ponraj S","Naresh Raj","Raashi Chhalani","naveen","Praba")')
    #test = test.query('subject in ("Dhruva Murari","Ponraj S","Naresh Raj","Raashi Chhalani","naveen","Praba")')
    print 'the training data shape is ',train.shape
    print 'the test data shape is ', test.shape
    
    X_tr = []
    Y_tr = []
    iteration = 0
    for i in train.values[:,0]:
        #image = exposure.equalize_hist(resize(np.array(Image.open(i).convert('L')),(96,96)))
        image=resize(np.array(Image.open(i)),(224,224))
        image = cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
        #print image.shape
        X_tr.append(image)
        Y_tr.append(train.values[iteration,1])
        iteration+=1
        if iteration % 5000==0:
            print colored((float(iteration)/len(train.values[:,0])*100 ,' Percentage complete'), 'green')
            
            
    X_ts = []
    Y_ts = []
    iteration = 0
    for i in test.values[:,0]:
        #image = exposure.equalize_hist(resize(np.array(Image.open(i).convert('L')),(96,96)))
        image=resize(np.array(Image.open(i)),(224,224))
        image = cv2.cvtColor(image,cv2.COLOR_GRAY2BGR)
        X_ts.append(image)
        Y_ts.append(test.values[iteration,1])
        iteration+=1
        if iteration % 5000==0:
            print colored((float(iteration)/len(test.values[:,0])*100 ,' Percentage complete'), 'green')
    X_tr,X_ts,Y_tr,Y_ts = np.array(X_tr),np.array(X_ts),np.array(Y_tr),np.array(Y_ts)
    print 'the training file shape',X_tr.shape,Y_tr.shape
    print 'the testing file shape',X_ts.shape,Y_ts.shape
    
    return X_tr,X_ts,Y_tr,Y_ts

In [15]:
def simulate(X,Y):
    import scipy as sp
    import scipy.ndimage
    complete = np.zeros((1,96,96),dtype=np.uint8)
    Y_complete = []
    for i in range(len(X)):
        #complete = np.vstack((complete,X[i,:,:].reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -10,reshape=False,cval=255).reshape(1,96,96)))
        rotated = np.fliplr(X[i,:,:])
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -15,reshape=False,cval=255).reshape(1,96,96)))
        #complete = np.vstack((complete,rotated.reshape(1,96,96)))
        Y_complete = np.append(Y_complete,([Y[i]]*12))
        if i % 50==0:
            print colored((float(i)/len(X)*100 ,' Percentage complete'),'green')
    complete = complete[1:,:,:]
    return complete,Y_complete

In [16]:
def augment(X,Y):
    import skimage.transform as tf
    import scipy as sp
    import scipy.ndimage
    complete = np.zeros((1,96,96),dtype=np.uint8)
    Y_complete = []
    for i in range(len(X)):
        #complete = np.vstack((complete,X[i,:,:].reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(0,-5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(0,5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(-5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(0,-5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(0,5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(-5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(0,-5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(0,5))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(-5,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        
        tform = tf.SimilarityTransform(scale=1,translation=(0,-3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(0,3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1,translation=(-3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(0,-3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(0,3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=1.2,translation=(-3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(0,-3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(0,3))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.SimilarityTransform(scale=0.8,translation=(-3,0))
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))

        
        tform = tf.AffineTransform(scale=(1,1),shear=0.25)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=0.2)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=0.15)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=0.1)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=-0.25)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=-0.2)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=-0.15)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        tform = tf.AffineTransform(scale=(1,1),shear=-0.1)
        complete = np.vstack((complete,tf.warp(X[i,:,:], tform).reshape(1,96,96)))
        
        rotated = np.fliplr(X[i,:,:])
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = 15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(X[i,:,:], angle = -10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = 15,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -5,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -10,reshape=False,cval=255).reshape(1,96,96)))
        complete = np.vstack((complete,scipy.ndimage.rotate(rotated, angle = -15,reshape=False,cval=255).reshape(1,96,96)))
        
        
        Y_complete = np.append(Y_complete,([Y[i]]*44))
        if i % 50==0:
            print colored((float(i)/len(X)*100 ,' Percentage complete'),'green')
    complete = complete[1:,:,:]
    return complete,Y_complete

In [17]:
def Save_data(X,Y,data):
    for i in range(len(X)):
        file_name = '/home/mckc/face_'+data+'/'+Y[i]+'_'+str(i)+'.npy'
        np.save(file_name,X[i,:,:])

In [18]:
def load(data):
    import os
    import numpy as np
    files = os.listdir('/home/mckc/face_'+data+'/')    
    X = np.zeros((1,96,96),dtype=np.float64)
    Y = []
    iter = 0
    for i in files:
        X = np.vstack((X,np.load('/home/mckc/face_'+data+'/'+i).reshape(1,96,96)))
        index = i.index('_')
        Y = np.append(Y,i[:index])
        iter = iter+1
        if iter % 800 ==0:
            print colored((float(iter)/len(files)*100 ,' Percentage complete'), 'green')
            
    print X[1:,:,:].shape,Y.shape
    return X[1:,:,:],Y
#from cv2 import resize from skimage import exposure import os import cv2 import numpy as np images_geq = [] images = [] label=[] files = os.listdir('/home/mckc/aug/face_labels/All') for i in files: image =cv2.resize(cv2.imread('/home/mckc/aug/face_labels/All/'+i,0),(96,96)) image_geq = exposure.equalize_hist(image) # Equalization images.append(image) images_geq.append(image_geq) label.append(i) image_data = np.array(images).reshape(-1,9216) images_geq = np.array(images_geq).reshape(-1,9216)

In [19]:
X_train,X_ts,Y_train,Y_ts    = load_data()
#X_tr,X_ts,Y_tr,Y_ts    = load_data()


the training data shape is  (25485, 2)
the test data shape is  (9413, 2)
(19.619383951343927, ' Percentage complete')
(39.238767902687854, ' Percentage complete')
(58.85815185403178, ' Percentage complete')
(78.47753580537571, ' Percentage complete')
(98.09691975671964, ' Percentage complete')
(53.11802825879104, ' Percentage complete')
the training file shape (25485, 224, 224, 3) (25485,)
the testing file shape (9413, 224, 224, 3) (9413,)
a = X_train[40] plt.imshow(a)
cv2.ellipse(a, (48,48), (50,80),0,0,360 ,0,20) plt.imshow(a,cmap=cm.Greys_r)

In [20]:
%%time
#X_tr,Y_tr = simulate(X_train,Y_train)
#X_tr,Y_tr = augment(X_train,Y_train)
X_tr,Y_tr = (X_train,Y_train)
print X_tr.shape,Y_tr.shape


(25485, 224, 224, 3) (25485,)
CPU times: user 0 ns, sys: 8 ms, total: 8 ms
Wall time: 8.19 ms

In [21]:
from sklearn.utils import shuffle
X_tr,Y_tr = shuffle(X_tr,Y_tr)
#X_normal = X_tr.reshape(-1,9216)
#X_test_normal = X_ts.reshape(-1,9216)
map, Y_number = np.unique(Y_tr, return_inverse=True)
Y_test_number = np.unique(Y_ts, return_inverse=True)[1]
Y_number = Y_number.astype("|S6")
len(map)
Y_test_number = Y_test_number.astype("|S6")
np.save('/home/mckc/map_big',map)

In [22]:
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
import pickle
#alexnet is RGB from keras.optimizers import SGD from convnetskeras.convnets import preprocess_image_batch, convnet model = convnet('alexnet',weights_path="/home/mckc/Downloads//alexnet_weights.h5", heatmap=False) model.layers.pop() model.add(Dense(2691, activation='softmax')) adam = Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08) model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
print('started') model.fit(X_tr.reshape(-1,3,224,224), Y_Keras, nb_epoch=500, batch_size=10,verbose=1 ,validation_data=(X_ts.reshape(-1,3,224,224),Y_Keras_test )) Y_kr= model.predict_classes(X_ts.reshape(-1,3,224,224)) print 'Accuracy of the model is ',accuracy_score(Y_ts,map[Y_kr],'\n') #confusion_matrix(Y_ts,Y_kr_vales) plot_confusion_matrix(map[Y_kr],Y_ts, classes=map,normalize=False,title='Confusion matrix')

In [24]:
#VGG is BGR
from keras.models import Sequential
from keras.utils import np_utils
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD

Y_Keras = np_utils.to_categorical(Y_number, 2691)
Y_Keras_test = np_utils.to_categorical(Y_test_number,2691)

def VGG_16(weights_path=None):
    model = Sequential()
    model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1,1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2,2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    if weights_path:
        model.load_weights(weights_path)

    return model

model = VGG_16('/home/mckc/Downloads/vgg16_weights.h5')
model = VGG_16()

model.layers.pop()
model.add(Dense(2691, activation='softmax'))

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')

In [25]:
print('started')
model.fit(X_tr.reshape(-1,3,224,224), Y_Keras, nb_epoch=500, batch_size=10,verbose=1
         ,validation_data=(X_ts.reshape(-1,3,224,224),Y_Keras_test ))

Y_kr= model.predict_classes(X_ts.reshape(-1,3,224,224))

print 'Accuracy of the model is ',accuracy_score(Y_ts,map[Y_kr],'\n')
#confusion_matrix(Y_ts,Y_kr_vales)
plot_confusion_matrix(map[Y_kr],Y_ts, classes=map,normalize=False,title='Confusion matrix')


started
Train on 25485 samples, validate on 9413 samples
Epoch 1/500
25485/25485 [==============================] - 15411s - loss: 6.9477 - val_loss: 6.9741
Epoch 2/500
25485/25485 [==============================] - 15412s - loss: 6.8049 - val_loss: 6.9807
Epoch 3/500
25485/25485 [==============================] - 15409s - loss: 6.7924 - val_loss: 6.9910
Epoch 4/500
25485/25485 [==============================] - 15408s - loss: 6.7918 - val_loss: 6.9910
Epoch 5/500
14150/25485 [===============>..............] - ETA: 6256s - loss: 6.7795
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-25-fbd6d44addef> in <module>()
      1 print('started')
      2 model.fit(X_tr.reshape(-1,3,224,224), Y_Keras, nb_epoch=500, batch_size=10,verbose=1
----> 3          ,validation_data=(X_ts.reshape(-1,3,224,224),Y_Keras_test ))
      4 
      5 Y_kr= model.predict_classes(X_ts.reshape(-1,3,224,224))

/home/mckc/anaconda/lib/python2.7/site-packages/keras/models.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, **kwargs)
    650                               shuffle=shuffle,
    651                               class_weight=class_weight,
--> 652                               sample_weight=sample_weight)
    653 
    654     def evaluate(self, x, y, batch_size=32, verbose=1,

/home/mckc/anaconda/lib/python2.7/site-packages/keras/engine/training.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch)
   1109                               val_f=val_f, val_ins=val_ins, shuffle=shuffle,
   1110                               callback_metrics=callback_metrics,
-> 1111                               initial_epoch=initial_epoch)
   1112 
   1113     def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

/home/mckc/anaconda/lib/python2.7/site-packages/keras/engine/training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics, initial_epoch)
    824                 batch_logs['size'] = len(batch_ids)
    825                 callbacks.on_batch_begin(batch_index, batch_logs)
--> 826                 outs = f(ins_batch)
    827                 if type(outs) != list:
    828                     outs = [outs]

/home/mckc/anaconda/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in __call__(self, inputs)
    809     def __call__(self, inputs):
    810         assert type(inputs) in {list, tuple}
--> 811         return self.function(*inputs)
    812 
    813 

/home/mckc/anaconda/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    857         t0_fn = time.time()
    858         try:
--> 859             outputs = self.fn()
    860         except Exception:
    861             if hasattr(self.fn, 'position_of_error'):

KeyboardInterrupt: 
map = np.load('/home/mckc/map.npy') loaded_model = pickle.load(open('/home/mckc/Face_code/logistic.pkl', 'rb')) Y_prob = clf.predict_proba(image_geq.reshape(-1,9216)) a= np.array([map[i] for i in np.argsort(Y_prob,axis=1)],dtype=np.chararray)[:,:4] zip(label==a[:,0])

In [33]:
clf = LogisticRegression(verbose=0,n_jobs=-1,multi_class='multinomial',solver='lbfgs',max_iter=50,warm_start=True)
clf.fit(X_normal,Y_number)
Y_logictic= clf.predict(X_ts.reshape(-1,9216))
#confusion_matrix(map[Y_logictic],Y_ts,labels=map)
print 'Accuracy of the model is ',accuracy_score(map[Y_logictic.astype(int)],Y_ts)
plot_confusion_matrix(map[Y_logictic.astype(int)],Y_ts, classes=map,normalize=True,title='Confusion matrix')


Accuracy of the model is  0.331031552109
Normalized confusion matrix
#Fishers recognizer = cv2.createLBPHFaceRecognizer() recognizer.train(X_tr, Y_number.astype(int)) Y_LBP = [recognizer.predict(X_ts[i])[0] for i in range(X_ts.shape[0])] print 'Accuracy of the model is ',accuracy_score(map[Y_logictic.astype(int)],Y_ts) #plot_confusion_matrix(map[Y_LBP],Y_ts, classes=map,normalize=True,title='Confusion matrix')
%%time Y_LBP = [recognizer.predict(X_ts[i])[0] for i in range(X_ts.shape[0])]

In [13]:
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten,AveragePooling2D
from keras.layers import Convolution2D, MaxPooling2D,BatchNormalization
from keras.utils import np_utils
from keras.optimizers import Adam,SGD,Adadelta,Adagrad
from keras import backend as K

Y_Keras = np_utils.to_categorical(Y_number, 2691)
Y_Keras_test = np_utils.to_categorical(Y_test_number,2691)

model = Sequential()
model.add(Convolution2D(32, 3, 3,border_mode='same',input_shape=(1, 96, 96)))
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(64, 3, 3,border_mode='same'))
convout2 = Activation('relu')
model.add(convout2)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.5))

model.add(Convolution2D(64, 3, 3,border_mode='same'))
convout3 = Activation('relu')
model.add(convout3)
model.add(Convolution2D(128, 3, 3,border_mode='same'))
convout4 = Activation('relu')
model.add(convout4)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Dropout(0.5))

model.add(Convolution2D(96, 3, 3,border_mode='same'))
convout5 = Activation('relu')
model.add(convout5)
model.add(Convolution2D(192, 3, 3,border_mode='same'))
convout6 = Activation('relu')
model.add(convout6)
model.add(BatchNormalization(epsilon=1e-05,axis=1,momentum=0.99))
model.add(MaxPooling2D((2,2), strides=(2,2)))

#model.add(Convolution2D(128, 3, 3,border_mode='same'))
#convout7 = Activation('relu')
#model.add(convout7)
#model.add(Convolution2D(256, 3, 3,border_mode='same'))
#convout8 = Activation('relu')
#model.add(convout8)
#model.add(MaxPooling2D((2,2), strides=(2,2)))

#model.add(Convolution2D(160, 3, 3,border_mode='same'))
#convout9 = Activation('relu')
#model.add(convout9)
#model.add(Convolution2D(320, 3, 3,border_mode='same'))
#convout10 = Activation('relu')
#model.add(convout10)
#model.add(AveragePooling2D(pool_size=(2, 2), strides=(1,1)))


model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(2691,activation='relu'))
model.add(Dense(2691,activation='softmax'))

adam = Adam(lr=0.00001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)

model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])


Using Theano backend.
Using gpu device 0: Quadro M2000M (CNMeM is enabled with initial size: 10.0% of memory, cuDNN 5005)
print('started') model.fit(X_tr.reshape(-1,1,96,96), Y_Keras, nb_epoch=500, batch_size=40,verbose=1 ,validation_data=(X_ts.reshape(-1,1,96,96),Y_Keras_test )) Y_kr= model.predict_classes(X_ts.reshape(-1,1,96,96)) print 'Accuracy of the model is ',accuracy_score(Y_ts,map[Y_kr],'\n') #confusion_matrix(Y_ts,Y_kr_vales) plot_confusion_matrix(map[Y_kr],Y_ts, classes=map,normalize=False,title='Confusion matrix')

In [14]:
%%time
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    rotation_range=40,  # randomly rotate images in the range (degrees, 0 to 180)
    width_shift_range=0.3,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.3,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=True,  # randomly flip images
    vertical_flip=False)  # randomly flip images

datagen.fit(X_tr.reshape(-1,1,96,96))
model.fit_generator(datagen.flow(X_tr.reshape(-1,1,96,96), Y_Keras,
                    batch_size=40), nb_epoch=500,verbose=1,
        samples_per_epoch = 20000,validation_data=(X_ts.reshape(-1,1,96,96),Y_Keras_test ))


Epoch 1/500
20000/20000 [==============================] - 1021s - loss: 7.7044 - acc: 0.0091 - val_loss: 10.8352 - val_acc: 2.1247e-04
Epoch 2/500
19965/20000 [============================>.] - ETA: 1s - loss: 7.0655 - acc: 0.0127
/home/mckc/anaconda/lib/python2.7/site-packages/keras/engine/training.py:1470: UserWarning: Epoch comprised more than `samples_per_epoch` samples, which might affect learning results. Set `samples_per_epoch` correctly to avoid this warning.
  warnings.warn('Epoch comprised more than '
20005/20000 [==============================] - 1022s - loss: 7.0651 - acc: 0.0127 - val_loss: 8.7126 - val_acc: 0.0021
Epoch 3/500
20005/20000 [==============================] - 1022s - loss: 6.9212 - acc: 0.0146 - val_loss: 8.6094 - val_acc: 0.0137
Epoch 4/500
20005/20000 [==============================] - 1022s - loss: 6.8159 - acc: 0.0188 - val_loss: 9.4261 - val_acc: 0.0168
Epoch 5/500
20000/20000 [==============================] - 1022s - loss: 6.7605 - acc: 0.0210 - val_loss: 10.0782 - val_acc: 0.0190
Epoch 6/500
20005/20000 [==============================] - 1022s - loss: 6.7073 - acc: 0.0231 - val_loss: 10.8852 - val_acc: 0.0168
Epoch 7/500
18285/20000 [==========================>...] - ETA: 78s - loss: 6.6563 - acc: 0.0220
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-14-83def3cd7e8c> in <module>()
----> 1 get_ipython().run_cell_magic(u'time', u'', u'from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n    featurewise_center=False,  # set input mean to 0 over the dataset\n    samplewise_center=False,  # set each sample mean to 0\n    featurewise_std_normalization=False,  # divide inputs by std of the dataset\n    samplewise_std_normalization=False,  # divide each input by its std\n    zca_whitening=False,  # apply ZCA whitening\n    rotation_range=40,  # randomly rotate images in the range (degrees, 0 to 180)\n    width_shift_range=0.3,  # randomly shift images horizontally (fraction of total width)\n    height_shift_range=0.3,  # randomly shift images vertically (fraction of total height)\n    horizontal_flip=True,  # randomly flip images\n    vertical_flip=False)  # randomly flip images\n\ndatagen.fit(X_tr.reshape(-1,1,96,96))\nmodel.fit_generator(datagen.flow(X_tr.reshape(-1,1,96,96), Y_Keras,\n                    batch_size=40), nb_epoch=500,verbose=1,\n        samples_per_epoch = 20000,validation_data=(X_ts.reshape(-1,1,96,96),Y_Keras_test ))')

/home/mckc/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_cell_magic(self, magic_name, line, cell)
   2118             magic_arg_s = self.var_expand(line, stack_depth)
   2119             with self.builtin_trap:
-> 2120                 result = fn(magic_arg_s, cell)
   2121             return result
   2122 

<decorator-gen-60> in time(self, line, cell, local_ns)

/home/mckc/anaconda/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/home/mckc/anaconda/lib/python2.7/site-packages/IPython/core/magics/execution.pyc in time(self, line, cell, local_ns)
   1175         else:
   1176             st = clock2()
-> 1177             exec(code, glob, local_ns)
   1178             end = clock2()
   1179             out = None

<timed exec> in <module>()

/home/mckc/anaconda/lib/python2.7/site-packages/keras/models.pyc in fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe, **kwargs)
    905                                         max_q_size=max_q_size,
    906                                         nb_worker=nb_worker,
--> 907                                         pickle_safe=pickle_safe)
    908 
    909     def evaluate_generator(self, generator, val_samples,

/home/mckc/anaconda/lib/python2.7/site-packages/keras/engine/training.pyc in fit_generator(self, generator, samples_per_epoch, nb_epoch, verbose, callbacks, validation_data, nb_val_samples, class_weight, max_q_size, nb_worker, pickle_safe, initial_epoch)
   1449                     outs = self.train_on_batch(x, y,
   1450                                                sample_weight=sample_weight,
-> 1451                                                class_weight=class_weight)
   1452                 except:
   1453                     _stop.set()

/home/mckc/anaconda/lib/python2.7/site-packages/keras/engine/training.pyc in train_on_batch(self, x, y, sample_weight, class_weight)
   1224             ins = x + y + sample_weights
   1225         self._make_train_function()
-> 1226         outputs = self.train_function(ins)
   1227         if len(outputs) == 1:
   1228             return outputs[0]

/home/mckc/anaconda/lib/python2.7/site-packages/keras/backend/theano_backend.pyc in __call__(self, inputs)
    809     def __call__(self, inputs):
    810         assert type(inputs) in {list, tuple}
--> 811         return self.function(*inputs)
    812 
    813 

/home/mckc/anaconda/lib/python2.7/site-packages/theano/compile/function_module.pyc in __call__(self, *args, **kwargs)
    857         t0_fn = time.time()
    858         try:
--> 859             outputs = self.fn()
    860         except Exception:
    861             if hasattr(self.fn, 'position_of_error'):

KeyboardInterrupt: 

In [15]:
model.save_weights("bigmodel_1.h5")

In [ ]:
import theano
convout1_f = theano.function([model.input], convout1.output)
convout2_f = theano.function([model.input], convout2.output)
convout3_f = theano.function([model.input], convout3.output)
convout4_f = theano.function([model.input], convout4.output)
convout5_f = theano.function([model.input], convout5.output)
convout6_f = theano.function([model.input], convout6.output)
#convout7_f = theano.function([model.input], convout7.output)
#convout8_f = theano.function([model.input], convout8.output)
#convout9_f = theano.function([model.input], convout9.output)
#convout10_f = theano.function([model.input], convout10.output)

# utility functions
from mpl_toolkits.axes_grid1 import make_axes_locatable

def nice_imshow(ax, data, vmin=None, vmax=None, cmap=None):
    """Wrapper around pl.imshow"""
    if cmap is None:
        cmap = cm.jet
    if vmin is None:
        vmin = data.min()
    if vmax is None:
        vmax = data.max()
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    im = ax.imshow(data, vmin=vmin, vmax=vmax, interpolation='nearest', cmap=cmap)
    plt.colorbar(im, cax=cax)

In [ ]:
import numpy.ma as ma
def make_mosaic(imgs, nrows, ncols, border=1):
    """
    Given a set of images with all the same shape, makes a
    mosaic with nrows and ncols
    """
    nimgs = imgs.shape[0]
    imshape = imgs.shape[1:]
    
    mosaic = ma.masked_all((nrows * imshape[0] + (nrows - 1) * border,
                            ncols * imshape[1] + (ncols - 1) * border),
                            dtype=np.float32)
    
    paddedh = imshape[0] + border
    paddedw = imshape[1] + border
    for i in xrange(nimgs):
        row = int(np.floor(i / ncols))
        col = i % ncols
        
        mosaic[row * paddedh:row * paddedh + imshape[0],
               col * paddedw:col * paddedw + imshape[1]] = imgs[i]
    return mosaic

#pl.imshow(make_mosaic(np.random.random((9, 10, 10)), 3, 3, border=1))

In [14]:
import matplotlib.pyplot as plt
%matplotlib inline
i = 12

# Visualize the first layer of convolutions on an input image
sample = X_ts[i:i+1]

plt.figure()
plt.title('input')
nice_imshow(plt.gca(), np.squeeze(sample), vmin=0, vmax=1, cmap=cm.Greys_r)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-14-d51a373a7431> in <module>()
      8 plt.figure()
      9 plt.title('input')
---> 10 nice_imshow(plt.gca(), np.squeeze(sample), vmin=0, vmax=1, cmap=cm.Greys_r)

NameError: name 'nice_imshow' is not defined

In [27]:
# Visualize convolution result (after activation)
C1 = convout1_f(sample.astype(np.float32).reshape(-1,1,96,96))
C1 = np.squeeze(C1)
print("C1 shape : ", C1.shape)

plt.figure(figsize=(15, 15))
plt.suptitle('convout1')
nice_imshow(plt.gca(), make_mosaic(C1, 6, 6), cmap=cm.Greys_r)


('C1 shape : ', (32, 96, 96))

In [15]:
from keras.models import model_from_json,model_from_yaml
# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
print("Saved model to disk")


Saved model to disk

In [15]:
%%time
from keras.models import model_from_json,model_from_yaml
json_file = open('/home/mckc/Face_code/model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("/home/mckc/Face_code/model.h5")
print("Loaded model from disk")


Loaded model from disk
CPU times: user 1.71 s, sys: 268 ms, total: 1.98 s
Wall time: 4.14 s

In [ ]:
from keras.models import model_from_json,model_from_yaml
json_file = open('/home/pi/Desktop/files/model_backup.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model

In [17]:
# serialize model to YAML
model_yaml = loaded_model.to_yaml()
with open("model.yaml", "w") as yaml_file:
    yaml_file.write(model_yaml)

In [18]:
# load YAML and create model
yaml_file = open('/home/pi/Desktop/files/model.yaml', 'r')
loaded_model_yaml = yaml_file.read()
yaml_file.close()
loaded_model = model_from_yaml(loaded_model_yaml)

# load weights into new model
loaded_model.load_weights("/home/mckc/Face_code/model.h5")
print("Loaded model from disk")


Loaded model from disk

In [ ]: